package com.example.gobang.game;

import com.example.gobang.model.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

@Component
public class Matcher {

    private Queue<User> normalQueue = new LinkedList<>();
    private Queue<User> highQueue = new LinkedList<>();
    private Queue<User> veryHighQueue = new LinkedList<>();

    @Autowired
    private OnlineUserManager onlineUserManager;

    private ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private RoomManager roomManager;

    public void add(User user) {
        if(user.getScore() < 2000) {
            synchronized (normalQueue) {
                normalQueue.offer(user);
                normalQueue.notify();
            }
            System.out.println("把玩家" + user.getUsername() + "加入到了normalQueue中!");
        }else if(user.getScore() >= 2000 && user.getScore() < 3000) {
            synchronized (highQueue) {
                highQueue.offer(user);
                highQueue.notify();
            }

            System.out.println("把玩家" + user.getUsername() + "加入到了highQueue中!");
        }else {
            synchronized (veryHighQueue) {
                veryHighQueue.offer(user);
                veryHighQueue.notify();
            }
            System.out.println("把玩家" + user.getUsername() + "加入到了veryHighQueue中!");
        }
    }

    public void remove(User user) {
        if(user.getScore() < 2000) {
            synchronized (normalQueue) {
                normalQueue.remove(user);

            }
            System.out.println("把玩家" + user.getUsername() + "移除了normalQueue中!");

        }else if(user.getScore() >= 2000 && user.getScore() < 3000) {
            synchronized (highQueue) {
                highQueue.remove(user);

            }
            System.out.println("把玩家" + user.getUsername() + "移除了highQueue中!");

        }else {
            synchronized (veryHighQueue) {
                veryHighQueue.remove(user);

            }
            System.out.println("把玩家" + user.getUsername() + "移除了veryHighQueue中!");
        }
    }

    public Matcher() {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                while (true) {
                    handleMatch(normalQueue);
                }
            }
        };
        t1.start();

        Thread t2 = new Thread(){
            @Override
            public void run() {
                while (true) {
                    handleMatch(highQueue);
                }
            }
        };
        t2.start();

        Thread t3 = new Thread(){
            @Override
            public void run() {
                while (true) {
                    handleMatch(veryHighQueue);
                }
            }
        };
        t3.start();

    }

    private void handleMatch(Queue<User> matchQueue) {
        synchronized (matchQueue) {
            try {
                while (matchQueue.size() < 2) {
                    matchQueue.wait();
                }
                User player1 = matchQueue.poll();
                User player2 = matchQueue.poll();
                System.out.println("匹配出两个玩家: " + player1.getUsername() + "," + player2.getUsername());

                WebSocketSession session1 = onlineUserManager.getFromGameHall(player1.getUserId());
                WebSocketSession session2 = onlineUserManager.getFromGameHall(player2.getUserId());

                if(session1 == null) {
                    matchQueue.offer(player2);
                    return;
                }
                if(session2 == null) {
                    matchQueue.offer(player1);
                    return;
                }
                if(session1 == session2) {
                    matchQueue.offer(player1);
                    return;
                }

                Room room = new Room();
                roomManager.add(room,player1.getUserId(),player2.getUserId());

                MatchResponse response1 = new MatchResponse();
                response1.setOk(true);
                response1.setMessage("matchSuccess");
                String json1 = objectMapper.writeValueAsString(response1);
                session1.sendMessage(new TextMessage(json1));

                MatchResponse response2 = new MatchResponse();
                response2.setOk(true);
                response2.setMessage("matchSuccess");
                String json2 = objectMapper.writeValueAsString(response2);
                session2.sendMessage(new TextMessage(json2));


            }catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}
